package com.Rolin.wss.controller;

import com.Rolin.wss.model.*;
import com.Rolin.wss.model.JspToController.SeatBO;
import com.Rolin.wss.service.FlightService;
import com.Rolin.wss.service.MealService;
import com.Rolin.wss.service.SeatService;
import com.Rolin.wss.util.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
@RequestMapping(value = "/features")
public class FeaturesController {

    @Autowired
    FlightService flightService;
    @Autowired
    MealService mealService;
    @Autowired
    SeatService seatService;

    @RequestMapping(value = "/doing/{type}",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public  String featuresHandle(@PathVariable String type, HttpSession session, Model model){
        UserVO user = (UserVO)session.getAttribute("user");
        model.addAttribute("userCome","<a class=\"come\">欢迎您，"+user.getUser_phone()+"</a><a class=\"come\" href=\"/wss/user/logout\"  style=\"margin-right:60px\">登出</a>");

        model.addAttribute("flight",flightService.getFlightInfo(1));
        model.addAttribute("type","other");


        //如果从拦截器过来的则直接返回视图
        if(type.equals("other")) return "order_choose";

        //从Session获取票号，如果没有则进入选票界面，如果有则进入相应的页面
        Integer ticket_No = (Integer) session.getAttribute("Ticket");

        if (ticket_No == null){
            model.addAttribute("type",type);
            return "order_choose";
        }else{
            return "redirect:/features/"+type;
        }
    }

    /**
     * 选票阶段的AJAX请求
     * 如果用户已经购买该机票则将机票ID写入Session
     * 如果用户没用购买该机票 ，则提示用户是否购买，如果用户选择购买
     * 则跳转到下一个cotroller处理，
     * @param ids 机票ID
     * @param session session
     * @return 返回一个代表状态和信息的JSON字符串
     */
    @RequestMapping(value = "/choose-ticket.do" ,method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String ticketToSession (@RequestParam String ids,HttpSession session) {
        int id = Integer.parseInt(ids);
        UserVO user = (UserVO) session.getAttribute("user");
        int res = flightService.havePurchasedTicket(id, user.getUser_id());
        if (res == -1) {//找不到这张票
            return utils.buildJSONForReturn(1, "你还没有购买该航班的机票，是否立即购买？");
        } else {
            session.setAttribute("Flight",id); //航班订单
            session.setAttribute("Ticket",res);//机票订单
            return utils.buildJSONForReturn(0, "选择成功");
        }
    }

    /**
     * 通过AJAX执行买票操作
     * @param ids 航班ID(请求参数)
     * @param session (Session)
     * @return 返回一个JSON字符串 0代表购买失败，1代表购买成功
     */
    @RequestMapping(value = "/buy-ticket.do",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String buyTicket(@RequestParam String ids,HttpSession session){
        int id = Integer.parseInt(ids);
        UserVO user = (UserVO) session.getAttribute("user");
        boolean isSuccess = flightService.buyTicket(id,user.getUser_id());
        if(isSuccess){

            session.setAttribute("Flight",id); //航班订单
            session.setAttribute("Ticket",flightService.havePurchasedTicket(id, user.getUser_id()));//机票订单

            return utils.buildJSONForReturn(1,"购买成功");
        }else{
            return utils.buildJSONForReturn(0,"购买出错");
        }
    }

    @RequestMapping(value = "/meals/{type}")
    public String mealMain(HttpSession session, @PathVariable String type,Model model){
        //对性别进行处理
        String sex;
        if( ((UserVO)session.getAttribute("user")).getUser_sex() == 0 ) model.addAttribute("user_sex","男");
        else model.addAttribute("user_sex","女");
        //对机票信息进行处理
        int ticket = (int)session.getAttribute("Ticket");
        FlightPOJO flightPOJO = flightService.getTicketInfo(ticket);
        model.addAttribute("ticket",flightPOJO);

        if (type.equals("free")) { //免费页面
            //对餐品视图进行处理
            model.addAttribute("meals",mealService.getMealList(0));
            return "meals_free";
        } else if (type.equals("pro")) { //付费页面
            //对餐品视图进行处理
            model.addAttribute("main",mealService.getMealList(1));
            model.addAttribute("second",mealService.getMealList(2));
            model.addAttribute("drink",mealService.getMealList(3));
            return "meals_pro";
        }
        else return "meals";
    }

    @RequestMapping(value = "/meals")
    public String mealMain2(HttpSession session,Model model){
        int Ticket = (int) session.getAttribute("Ticket");
        model.addAttribute("ticket",flightService.getTicketInfo(Ticket));
        return "meals";
    }


    @RequestMapping(value = "/center")
    public String intoCenter(HttpSession session,Model model){
        //获取票号
        int Ticket = (int) session.getAttribute("Ticket");
        FlightPOJO fpojo = flightService.getTicketInfo(Ticket);
        model.addAttribute("ticket",flightService.getTicketInfo(Ticket));
        UserVO user = (UserVO) session.getAttribute("user");

        //获取座位信息
        SeatOrderVO seatOrderVO = seatService.getSeatInUser(user.getUser_id(),fpojo.getFo_flight().getFlight_id());
        model.addAttribute("seat",seatOrderVO);

        //获取餐品信息
        MealOrderVO moo = mealService.getOrderedAMeal(Ticket);
        if(moo == null) {
            model.addAttribute("meal","暂未选餐");
            model.addAttribute("mealPrice","-");
        }else{
            if(moo.getMo_type()==0) {
                model.addAttribute("meal","特惠餐-"+mealService.getMeal(moo.getMo_food1()).getMeal_name());
                model.addAttribute("mealPrice","特惠餐由航班免费提供");
            }else{
                MealVO food1 = mealService.getMeal(moo.getMo_food1());
                MealVO food2 = mealService.getMeal(moo.getMo_food2());
                MealVO food3 = mealService.getMeal(moo.getMo_food3());

                model.addAttribute("meal",food1.getMeal_name()+"+"+food2.getMeal_name()+"+"+food3.getMeal_name());
                model.addAttribute("mealPrice",String.valueOf(food1.getMeal_price()+food2.getMeal_price()+food3.getMeal_price())+" 元");
            }
        }
        return "center";
    }

    @RequestMapping(value = "/seat")
    public String intoSeat(HttpSession session,Model model){
        //获取票号
        int Ticket = (int) session.getAttribute("Ticket");
        FlightPOJO fpojo = flightService.getTicketInfo(Ticket);
        model.addAttribute("ticket",fpojo);

        //获取已被占领的座位
        ArrayList<SeatBO> sbos = seatService.getSeatInFlight(fpojo.getFo_flight().getFlight_id());
        model.addAttribute("stop",sbos);

        return "seatChoice";
    }

}
